package com.ruoyi.service.impl;

import java.util.List;

import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mysql.cj.xdevapi.InsertStatementImpl;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.domain.Article;
import com.ruoyi.domain.ArticleUncheck;
import com.ruoyi.mapper.ArticleUncheckMapper;
import com.ruoyi.service.ArticleService;
import com.ruoyi.service.IArticleUncheckService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.mapper.RecoverStationMapper;
import com.ruoyi.domain.RecoverStation;
import com.ruoyi.service.IRecoverStationService;

/**
 * 回收站Service业务层处理
 * 
 * @author ya
 * @date 2025-08-01
 */
@Service
public class RecoverStationServiceImpl extends ServiceImpl<RecoverStationMapper,RecoverStation> implements IRecoverStationService
{
    @Autowired
    private RecoverStationMapper recoverStationMapper;

    @Autowired
    private ArticleService articleService;

    @Autowired
    private IRecoverStationService recoverStationService;

    @Autowired
    private IArticleUncheckService articleUncheckService;
    @Autowired
    private ArticleUncheckMapper articleUncheckMapper;

    @Override
    public int recoverData(Long[] recoverIds) {
        int count = 0;
        for (Long recoverId : recoverIds) {
            // 查询回收站记录
            RecoverStation recoverStation = recoverStationService.getById(recoverId);
            if (recoverStation != null) {
                // 判断需要恢复到哪个表
                if (recoverStation.getArticleId() != null) {
                    // 恢复到article表
                    Article article = new Article();
                    // 复制字段
                    article.setArticleId(recoverStation.getArticleId());
                    if (recoverStation.getTitle() != null)
                        article.setTitle(recoverStation.getTitle());
                    if (recoverStation.getArticleOrigin() != null)
                        article.setArticleOrigin(recoverStation.getArticleOrigin());
                    if (recoverStation.getColumnId() != null)
                        article.setColumnId(recoverStation.getColumnId());
                    if (recoverStation.getContent() != null)
                        article.setContent(recoverStation.getContent());
                    if (recoverStation.getAuthor() != null)
                        article.setAuthor(recoverStation.getAuthor());
                    if (recoverStation.getCreateBy() != null)
                        article.setCreateBy(recoverStation.getCreateBy());
                    if (recoverStation.getUpdateBy() != null)
                        article.setUpdateBy(recoverStation.getUpdateBy());
                    if (recoverStation.getUpdateTime() != null)
                        article.setUpdateTime(recoverStation.getUpdateTime());
                    if (recoverStation.getCreateTime() != null)
                        article.setCreateTime(recoverStation.getCreateTime());
                    if (recoverStation.getRemark() != null)
                        article.setRemark(recoverStation.getRemark());
                    if (recoverStation.getCoverPath() != null)
                        article.setCoverPath(recoverStation.getCoverPath());
                    if (recoverStation.getIp() != null)
                        article.setIp(recoverStation.getIp());
                    if (recoverStation.getPubdate() != null)
                        article.setPubdate(recoverStation.getPubdate());
                    if (recoverStation.getAppval() != null)
                        article.setAppval(recoverStation.getAppval());
                    if (recoverStation.getAuditId() != null)
                        article.setAuditId(recoverStation.getAuditId());
                    if (recoverStation.getLittleTitle1() != null)
                        article.setLittleTitle1(recoverStation.getLittleTitle1());
                    if (recoverStation.getLittleTitle2() != null)
                        article.setLittleTitle2(recoverStation.getLittleTitle2());
                    if (recoverStation.getStatus() != null)
                        article.setStatus(recoverStation.getStatus());
                    if (recoverStation.getAttachment() != null)
                        article.setAttachment(recoverStation.getAttachment());
                    //pdf图片路径
                    if (recoverStation.getPdfImagePaths() != null)
                        article.setPdfImagePaths(recoverStation.getPdfImagePaths());

                    // 检查文章是否还存在
                    Article existingArticle = articleService.getById(recoverStation.getArticleId());
                    if (existingArticle != null) {
                        // 如果文章还存在，更新文章状态
                        articleService.updateById(article);
                    } else {
                        // 如果文章不存在，插入新记录
                        articleService.save(article);
                    }

                } else if (recoverStation.getUncheckId() != null) {
                    // 恢复到article_uncheck表
                    ArticleUncheck articleUncheck = new ArticleUncheck();
                    // 复制字段
                    articleUncheck.setArticleId(recoverStation.getUncheckId());
                    if (recoverStation.getTitle() != null)
                        articleUncheck.setTitle(recoverStation.getTitle());
                    if (recoverStation.getArticleOrigin() != null)
                        articleUncheck.setArticleOrigin(recoverStation.getArticleOrigin());
                    if (recoverStation.getColumnId() != null)
                        articleUncheck.setColumnId(recoverStation.getColumnId());
                    if (recoverStation.getContent() != null)
                        articleUncheck.setContent(recoverStation.getContent());
                    if (recoverStation.getAuthor() != null)
                        articleUncheck.setAuthor(recoverStation.getAuthor());
                    if (recoverStation.getCreateBy() != null)
                        articleUncheck.setCreateBy(recoverStation.getCreateBy());
                    if (recoverStation.getUpdateBy() != null)
                        articleUncheck.setUpdateBy(recoverStation.getUpdateBy());
                    if (recoverStation.getUpdateTime() != null)
                        articleUncheck.setUpdateTime(recoverStation.getUpdateTime());
                    if (recoverStation.getCreateTime() != null)
                        articleUncheck.setCreateTime(recoverStation.getCreateTime());
                    if (recoverStation.getRemark() != null)
                        articleUncheck.setRemark(recoverStation.getRemark());
                    if (recoverStation.getCoverPath() != null)
                        articleUncheck.setCoverPath(recoverStation.getCoverPath());
                    if (recoverStation.getIp() != null)
                        articleUncheck.setIp(recoverStation.getIp());
                    if (recoverStation.getPubdate() != null)
                        articleUncheck.setPubdate(DateUtils.dateTime(DateUtils.YYYY_MM_DD, recoverStation.getPubdate()));
                    if (recoverStation.getAppval() != null)
                        articleUncheck.setAppval(recoverStation.getAppval());
                    if (recoverStation.getState() != null)
                        articleUncheck.setState(recoverStation.getState());
                    if (recoverStation.getLittleTitle1() != null)
                        articleUncheck.setLittleTitle1(recoverStation.getLittleTitle1());
                    if (recoverStation.getLittleTitle2() != null)
                        articleUncheck.setLittleTitle2(recoverStation.getLittleTitle2());
                    if (recoverStation.getAttachment()!= null)
                        articleUncheck.setAttachment(recoverStation.getAttachment());
                    // 检查uncheck记录是否还存在
                    ArticleUncheck existingUncheck = articleUncheckMapper.selectArticleUncheckByArticleId(recoverStation.getUncheckId());
                    if (existingUncheck != null) {
                        // 如果记录还存在，更新
                        articleUncheckService.updateById(articleUncheck);
                    } else {
                        // 如果记录不存在，插入新记录
                        articleUncheckService.save(articleUncheck);
                    }
                }

                // 删除回收站记录
                recoverStationService.removeById(recoverId);
                count++;
            }
        }
        return count;
    }



    /**
     * 查询回收站
     * 
     * @param id 回收站主键
     * @return 回收站
     */
    @Override
    public RecoverStation selectRecoverStationById(Long id)
    {
        return recoverStationMapper.selectRecoverStationById(id);
    }

    /**
     * 查询回收站列表
     * 
     * @param recoverStation 回收站
     * @return 回收站
     */
    @Override
    public List<RecoverStation> selectRecoverStationList(RecoverStation recoverStation , String userName , boolean allowAll)
    {
        return recoverStationMapper.selectRecoverStationList(recoverStation,userName,allowAll);

    }

    /**
     * 新增回收站
     * 
     * @param recoverStation 回收站
     * @return 结果
     */
    @Override
    public int insertRecoverStation(RecoverStation recoverStation)
    {
        recoverStation.setCreateTime(DateUtils.getNowDate());
        // 设置默认有效期为30天
        if (recoverStation.getValidDate() == null) {
            recoverStation.setValidDate(30);
        }
        return recoverStationMapper.insertRecoverStation(recoverStation);
    }

    /**
     * 修改回收站
     * 
     * @param recoverStation 回收站
     * @return 结果
     */
    @Override
    public int updateRecoverStation(RecoverStation recoverStation)
    {
        recoverStation.setUpdateTime(DateUtils.getNowDate());
        return recoverStationMapper.updateRecoverStation(recoverStation);
    }

    /**
     * 批量删除回收站
     * 
     * @param ids 需要删除的回收站主键
     * @return 结果
     */
    @Override
    public int deleteRecoverStationByIds(Long[] ids)
    {
        return recoverStationMapper.deleteRecoverStationByIds(ids);
    }

    /**
     * 删除回收站信息
     * 
     * @param id 回收站主键
     * @return 结果
     */
    @Override
    public int deleteRecoverStationById(Long id)
    {
        return recoverStationMapper.deleteRecoverStationById(id);
    }
}
